package com.roadmap.struts.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.HibernateException;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.RdcConst;
import com.roadmap.exception.ActionNotSupportedException;
import com.roadmap.exception.InvalidQueryException;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.NotMatchedException;
import com.roadmap.exception.ServerException;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcAction;
import com.roadmap.struts.form.admin.PersonalStateForm;

public class StateAction extends RdcAction {

	public StateAction() {
		super("getstate");
	}

	@Override
	public ActionForward performAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/state.do");
		}

		String currentUserId = getUIDFromSession(request);
		if (currentUserId.equals(ownerId)) {
			IManager visionMgr = ManagerFactory.getInstance(RdcConst.DREAM,
					currentUserId, ownerId);
			PersonalStateForm psForm = new PersonalStateForm();
			request.setAttribute("personalStateForm", psForm);
			psForm.setHasVision(visionMgr.get(ownerId) != null);
		}
		
		retrieveExecutableGoals(currentUserId, ownerId, request);
		retrieveExecutableTasks(currentUserId, ownerId, request);
		return mapping.findForward("getstate");
	}

	private void retrieveExecutableTasks(String curUserId, String ownerId,
			HttpServletRequest request) throws HibernateException,
			ServerException, InvalidQueryException,
			ActionNotSupportedException, InvalidValueException,
			NotMatchedException {
		IManager mgr = ManagerFactory.getInstance(RdcConst.TASK, curUserId,
				ownerId);
		List list = mgr.getExecutableEntities();
		if (list == null || list.isEmpty()) {
			return;
		}

		request.setAttribute("tasks", list);

		PersonalStateForm psForm = (PersonalStateForm) request
				.getAttribute("personalStateForm");
		if (psForm != null) {
			psForm.setHasAvailableTask(true);
		}
	}

	private void retrieveExecutableGoals(String curUserId, String ownerId,
			HttpServletRequest request) throws InvalidQueryException,
			ActionNotSupportedException, InvalidValueException,
			ServerException, HibernateException, NotMatchedException {
		IManager mgr = ManagerFactory.getInstance(RdcConst.GOAL, curUserId,
				ownerId);
		List list = mgr.getExecutableEntities();
		if (list == null || list.isEmpty()) {
			return;
		}

		request.setAttribute("goals", list);

		PersonalStateForm psForm = (PersonalStateForm) request
				.getAttribute("personalStateForm");
		if (psForm != null) {
			psForm.setHasAvailableGoal(true);
		}
	}

	@Override
	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}

}
